package com.crowntit.domain.trade.service.lock;

import com.crowntit.domain.trade.adapter.repository.ITradeRepository;
import com.crowntit.domain.trade.model.aggregate.GroupBuyOrderAggregate;
import com.crowntit.domain.trade.model.entity.*;
import com.crowntit.domain.trade.model.valobj.GroupBuyProgressVO;
import com.crowntit.domain.trade.model.valobj.TradeOrderStatusEnumVO;
import com.crowntit.domain.trade.service.ITradeLockOrderService;
import com.crowntit.domain.trade.service.lock.factory.TradeLockRuleFilterFactory;
import com.crowntit.types.design.framework.link.model2.chain.BusinessLinkedLink;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Service
@Slf4j
public class TradeLockOrderService implements ITradeLockOrderService {

    @Resource
    private ITradeRepository tradeRepository;
    @Resource(name = "tradeLockRuleFilterLink")
    private BusinessLinkedLink<TradeLockRuleCommandEntity, TradeLockRuleFilterFactory.DynamicContext, TradeLockRuleBackEntity> tradeRuleFilterLink;

    @Override
    public LockPayOrderEntity queryMarketTradeOrderListByOutTradeNo(String source, String channel, String userId, String outTradeNo) {
        log.info("拼团交易-查询未支付营销订单:{} outTradeNo:{}", userId, outTradeNo);
        GroupBuyOrderListEntity groupBuyOrderListEntity = tradeRepository.queryUnPayMarketTradeOrderListByOutTradeNo(source, channel, userId, outTradeNo);
        if (groupBuyOrderListEntity == null)
            return null;

        GroupBuyTeamEntity groupBuyTeamEntity = tradeRepository.queryGroupBuyOrderTeamByTeamId(groupBuyOrderListEntity.getTeamId());
        return LockPayOrderEntity.builder()
                .outTradeNo(outTradeNo)
                .status(TradeOrderStatusEnumVO.get(groupBuyOrderListEntity.getStatus()))
                .deductionPrice(groupBuyOrderListEntity.getDeductionPrice())
                .payPrice(groupBuyOrderListEntity.getPayPrice())
                .validEndTime(groupBuyTeamEntity.getValidEndTime())
                .validStartTime(groupBuyTeamEntity.getValidStartTime()).build();
    }

    @Override
    public boolean isGroupBuyComplete(String teamId) {
        log.info("拼团交易-查询拼单进度:{}", teamId);
        GroupBuyProgressVO groupBuyProgressVO = tradeRepository.queryGroupBuyProgressByTeamId(teamId);
        if (groupBuyProgressVO == null)
            return false;

        return Objects.equals(groupBuyProgressVO.getTargetCount(), groupBuyProgressVO.getLockCount());
    }

    @Override
    public LockPayOrderEntity lockMarketTradeOrder(UserEntity userEntity, LockOrderActivityEntity activityEntity, LockOrderDiscountEntity discountEntity, NotifyEntity notifyEntity) throws Exception {
        log.info("拼团交易-拼团锁单");
        TradeLockRuleBackEntity tradeLockRuleBackEntity = tradeRuleFilterLink.apply(TradeLockRuleCommandEntity.builder()
                .activityId(activityEntity.getActivityId())
                .userId(userEntity.getUserId())
                .teamId(activityEntity.getTeamId())
                .build(), new TradeLockRuleFilterFactory.DynamicContext());

        userEntity.setUserTakeOrderCount(tradeLockRuleBackEntity.getUserTakeLimitCount());

        GroupBuyOrderAggregate groupBuyOrderAggregate = GroupBuyOrderAggregate.builder()
                .userEntity(userEntity)
                .activityEntity(activityEntity)
                .discountEntity(discountEntity)
                .notifyEntity(notifyEntity)
                .build();

        return tradeRepository.lockMarketTradeOrder(groupBuyOrderAggregate);
    }
}
